<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%water}}".
 *
 * @property int $id
 * @property int|null $userid 用户id
 * @property int|null $fund_type 资金类型
 * @property int|null $add_sub 加减
 * @property float|null $front 变化前
 * @property float|null $after 变化后
 * @property float|null $amount 变化金额
 * @property string|null $use 用途
 * @property string|null $remarks 备注
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 */
class Water extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%water}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['userid', 'fund_type', 'add_sub'], 'integer'],
            [['front', 'after', 'amount'], 'number'],
            [['remarks'], 'string'],
            [['create_time', 'update_time'], 'safe'],
            [['use'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'fund_type' => '资金类型',
            'add_sub' => '加减',
            'front' => '变化前',
            'after' => '变化后',
            'amount' => '变化金额',
            'use' => '用途',
            'remarks' => '备注',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
        ];
    }

    /**
     * 添加流水
     * @param $userid 用户id
     * @param $fund_type 流水资金类型 1:跑腿佣金 2:分销佣金
     * @param $add_sub 加减 1:加 2:减
     * @param $front 变化前
     * @param $amount 变化金额
     * @param $use 用途
     * @param string $remarks 备注
     * @return bool
     */
    public static function add($userid,$fund_type,$add_sub,$front,$amount,$use,$remarks='')
    {
        if ($add_sub==1){
            $after = $front+$amount;
        }else{
            $after = $front-$amount;
        }
        $model = new self();
        $model->userid = $userid;
        $model->fund_type = $fund_type;
        $model->add_sub = $add_sub;
        $model->front = $front;
        $model->amount = $amount;
        $model->after = $after;
        $model->use = $use;
        $model->remarks = $remarks;
        return $model->save();
    }
}
